package 多窗口卖票.多线程的同步代码块机制;
/**
 * 利用同步代码块方法处理Thread线程的安全问题
 */
class Window extends Thread{

    private static int ticket=100;
    private static Object obj=new Object();//
    @Override
    public void run() {
        while (true){
            synchronized (Window.class){//以当前类名作为同步锁
            //synchronized(obj) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName() + "卖的票号：" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}

public class Test2 {
    public static void main(String[] args) {

        //创建三个线程子类对象
        Window w1=new Window();
        Window w2=new Window();
        Window w3=new Window();


        //给三个线程该名称
        w1.setName("窗口一：");
        w2.setName("窗口二：");
        w3.setName("窗口三：");

        //调用start()方法
        w1.start();
        w2.start();
        w3.start();


    }
}
